#include "hserialport.h"
#include "qdebug.h"
#include "MDateTime/MDateTime.h"

#ifdef Q_OS_WIN
    // windows系统 精确延时 1ms
    #pragma comment(lib, "winmm.lib")
    #include <Windows.h>
    #include <MMSystem.h>
#elif defined(Q_OS_LINUX)
    // linux系统 后续用到的时候再补充
#elif defined(Q_OS_MAC)
    // mac系统 后续用到的时候再补充
#endif

HSerialPort::HSerialPort() {
    m_pSerialPortInfo = new QSerialPortInfo();

    // 设置模式为事件驱动模式
    m_pPort = new QSerialPort();
}

bool HSerialPort::open(QString port) {
    bool bFlag = true;
    if (!m_pPort->isOpen()) {
        m_pPort->setPortName(port);
        bFlag = m_pPort->open(QIODevice::ReadWrite);
    }
    return bFlag;
}

void HSerialPort::close()
{
    if(m_pPort->isOpen()){
        m_pPort->close();
    }
}

int HSerialPort::write(QByteArray &ucDataArray)
{
    if(!m_pPort->isOpen()){
        qDebug() << "请先打开串口……";
        return -1;
    }

    return m_pPort->write(ucDataArray.data());
}

QStringList HSerialPort::getAvailablePorts() {
    QStringList ports_available;
    foreach (QSerialPortInfo portinfo, m_pSerialPortInfo->availablePorts()) {
        ports_available.append(portinfo.portName());
    }
    return ports_available;
}

void HSerialPort::run()
{
    m_bStopped = true;
    int nAvailable  = 0;
    QByteArray  m_arrBuffer;
    m_arrBuffer.clear();

    while (m_bStopped) // 如果串口被成功初始化并且正确打开，否则就结束。
    {
#ifdef Q_OS_WIN
        // windows系统 精确延时 1ms
        timeBeginPeriod(1);
        Sleep(1);
        timeEndPeriod(1);
#elif defined(Q_OS_LINUX)
        // linux系统 后续用到的时候再补充
        sleep(1)
#elif defined(Q_OS_MAC)
        // mac系统 后续用到的时候再补充
#endif
        // qDebug() << "Timer timeout" << MDateTime::currentDateTime() << getTimeStamp();
        nAvailable = m_pPort->bytesAvailable();
        if (nAvailable > 0) {
            // m_arrBuffer.resize(nAvailable);

            m_arrBuffer.append(m_pPort->readAll());

            if (m_arrBuffer.size() > 0) {
                qDebug() << m_arrBuffer.toHex() << getTimeStamp();
                emit signal_initialData(false, m_arrBuffer, getTimeStamp());

                m_arrBuffer.clear();
            }
        }
    }
    m_bStopped = false;
}

quint64 HSerialPort::getTimeStamp()
{
    if(m_bUseXCLK){
        return XCLK;
    }
    else{
        return MDateTime::currentMSecsSinceEpoch();
    }
}





